import axios from 'axios'

import JsonBigint from 'json-bigint'
// action="http://ttapi.research.itcast.cn/mp/v1_0/user/images"
const request = axios.create({
  baseURL: 'http://ttapi.research.itcast.cn',
  transformResponse: [function (data) {
    try {
      return JsonBigint.parse(data)
    } catch {
      return data
    }
  }]
})

export const login = data => {
  return request.post('/mp/v1_0/authorizations', data)
}
request.interceptors.request.use(config => {
  const user = JSON.parse(window.sessionStorage.getItem('user'))
  if (user) {
    config.headers.Authorization = `Bearer ${user.token}`
  }
  return config
})
export const getUserProfile = () => {
  return request.get('/mp/v1_0/user/profile')
}
export const getArticles = params => {
  return request.get('/mp/v1_0/articles', { params: params })
}
export const getArticleChannels = () => {
  return request.get('/mp/v1_0/channels')
}
export const deleteArticle = (id) => {
  return request.delete('/mp/v1_0/articles/' + id)
}
export const addArticle = (data, draft) => {
  return request.post('/mp/v1_0/articles',
    data, { params: { draft: draft } })
}
// * 获取指定文章
export const getArticleById = (id) => {
  return request.get('/mp/v1_0/articles/' + id)
}
export const updateArticle = (id, data, draft) => {
  return request.put('/mp/v1_0/articles/' + id, data,
    {
      params: {
        draft: draft
      }
    })
}

export const uploadImage = data => {
  return request.post('/mp/v1_0/user/images', data)
}
export const getImages = params => {
  return request.get('/mp/v1_0/user/images', {
    params
  })
}
export const collectImage = (imageid, collect) => {
  return request.put('/mp/v1_0/user/images/' + imageid, {
    collect: collect
  })
}
export const deleteImage = imageId => {
  return request({
    method: 'DELETE',
    url: `/mp/v1_0/user/images/${imageId}`
  })
}
export const updateUserPhoto = data => {
  return request.patch('/mp/v1_0/user/photo', data)
}
// 修改用户基本信息
export const updateUserProfile = data => {
  return request.patch('/mp/v1_0/user/profile', data)
}
//  修改文章评论状态

export const updateCommentStatus = (articleId, allowComment) => {
  return request.put('/mp/v1_0/comments/status', {
    allow_comment: allowComment
  }, {
    params: {
      article_id: articleId
    }
  }
  )
}
export const getfanslist = (params) => {
  return request.get('/mp/v1_0/followers', {
    params
  })
}
